อยากใช้เครื่องหมาย Quote ใน String ทำยังไงได้บ้าง ?? มาดูกันเลย 🔥
.
👉 เมื่อเราต้องการกำหนด string ใน JavaScript ทำได้โดยกำหนดข้อความ หรือตัวอักษรให้อยู่ภายใต้ single quote (‘ ’) หรือ double quote (“ ”)
.
⚙️ ตัวอย่างเช่น
single quote (‘ ’)
let str1 = 'With Great power come great responsibility';
.
double quote (“ ”)
let str2 = "Your heart is so full of hatred. You are not fit to be king.";
ขอบคุณประโยคเด็ด ๆ จากภาพยนต์เรื่อง Spiderman และ เรื่อง Black Panther
.
👉 หากเราอยากให้มีเครื่อง Quote (‘ ’ หรือ “ ”) ไว้ใน String สามารถทำได้โดย 2 วิธี ดังนี้
.
🌟 1) ใช้เครื่องหมาย Backslash ( \ ) ก่อนหน้าเครื่องหมายคำพูด (“ ”)
📑 ตัวอย่าง 1
let str = '\"BorntoDev Academy\"';
console.log(str);
//"BorntoDev Academy"
.
📑 ตัวอย่าง 2
let myName = "He said, \" My name is Prayut Chan \".";
console.log(myName);
// He said, " My name is Prayut Chan ".
.
🌟 2) ใช้เครื่องหมาย backticks (`) แทน single quote (‘ ’) หรือ double quote (“ ”) ใน String
.
📑 ตัวอย่าง
let string1 = `We're safely using apostrophes and "quotes" in a template literal.`;
console.log(string1);
// We're safely using apostrophes and "quotes" in a template literal.
.
หวังว่าจะเป็นประโยชน์กับเพื่อน ๆ น้าาาา 🥰
.
borntoDev - 🦖 สร้างการเรียนรู้ที่ดีสำหรับสายไอทีในทุกวัน
#javascript #string #quotes #BorntoDev